//
//  ViewController.m
//  mutipeer广播
//
//  Created by MyMac on 15/9/11.
//  Copyright (c) 2015年 tongfang. All rights reserved.
//

#import "ViewController.h"
#import <MultipeerConnectivity/MultipeerConnectivity.h>
@interface ViewController ()<MCSessionDelegate,MCAdvertiserAssistantDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *messageImage;
@property (weak, nonatomic) IBOutlet UILabel *connectionState;
@property(nonatomic,strong)MCSession*session;
@property(nonatomic,strong)MCAdvertiserAssistant*advertiser;
@property(nonatomic,strong)NSMutableArray*peerIDArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.peerIDArray=[NSMutableArray array];
    MCPeerID*peerID=[[MCPeerID alloc]initWithDisplayName:[[UIDevice currentDevice] name]];
    //创建一个会话，发送和接收数据
    self.session=[[MCSession alloc]initWithPeer:peerID];
    self.session.delegate=self;
    
    //创建一个广播
    self.advertiser=[[MCAdvertiserAssistant alloc]initWithServiceType:@"cmj-stream" discoveryInfo:nil session:self.session];
    self.advertiser.delegate=self;


    
}
#pragma -mark MCAdvertiserAssistantDelegate
- (void)advertiserAssistantWillPresentInvitation:(MCAdvertiserAssistant *)advertiserAssistant;
{
    NSLog(@"广播将要邀请");

}


- (void)advertiserAssistantDidDismissInvitation:(MCAdvertiserAssistant *)advertiserAssistant;
{

    NSLog(@"广播停止邀请");


}
#pragma -mark MCSessionDelegate
//外面设备是否连接进来
//两个设备处在同一个wifi或蓝牙时才能链接上
//资源传输有三种方式，可以直接传文件，可以传文件地址，可以传资源
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
    switch (state) {
        case MCSessionStateNotConnected:
            self.connectionState.text=@"断开连接";
            NSLog(@"断开连接");
            break;
        case MCSessionStateConnected:{
           BOOL ret=[self.peerIDArray containsObject:peerID];
            if (!ret) {
                [self.peerIDArray addObject:peerID];
            }
            self.connectionState.text=@"已经连接";
            NSLog(@"已经连接");
        }
            break;
        case MCSessionStateConnecting:
            self.connectionState.text=@"正在连接";
            NSLog(@"正在连接");
        default:
            break;
    }

}



- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID;
{

    NSLog(@"已经接受数据:%@",peerID.displayName);
    self.messageImage.image=[UIImage imageWithData:data];
}


- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID;
{
    //会话可以发送多种类型的数据，，可以是流活着资源等
    NSLog(@"已经接受流ID：%@    streamName:%@",peerID.displayName,streamName);
    

}
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress;
{

    NSLog(@"开始接受资源resourceName:%@     ID:%@ ",resourceName,peerID.displayName);
}

- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error;
{

    NSLog(@"完成接受资源resourceName:%@   peerID:%@    localURL:%@     error:%@",resourceName,peerID.displayName,localURL,error);

}

- (IBAction)startAdv:(id)sender {
    //开始广播
    [self.advertiser start];
}
- (IBAction)stopAdv:(id)sender {
    [self.advertiser stop];
}
- (IBAction)isConnection:(id)sender {
    [self.session disconnect];
}
- (IBAction)writeData:(id)sender {
    
    UIImage*image=[UIImage imageNamed:@"屏幕快照 2015-09-11 下午4.43.20"];
    NSData*data=UIImagePNGRepresentation(image);
    NSError*error=nil;
    //注意，，，发送数据时可规定发送方式是可靠连接还是不可靠连接，而且可以群发，内部发送数据的连接被苹果屏蔽了，我们不知道
    [self.session sendData:data toPeers:[self.session connectedPeers] withMode:MCSessionSendDataReliable error:&error];
    
    
}


@end
